package com.itheima.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BeanServlet;
import com.itheima.domain.store.Company;
import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.Impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @param
 * @return
 */
@WebServlet("/system/dept/*")
public class DeptServlet extends BeanServlet {
    //创建业务层对象复用
    DeptService service=new DeptServiceImpl();
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端发送过来的id
        String id = req.getParameter("id");
        //调用业务层
        service.delete(id);
        this.findByPage(req,resp);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //使用工具类从前端获取修改的数据,自动封装为对象
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        //调用业务层的修改方法
        service.update(dept);
      this.findByPage(req,resp);

    }
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据传递过来的id查询对应的部门信息，以供回显操作
        String id = req.getParameter("id");
        Dept dept=service.findById(id);
        //获取所有部门信息
        List<Dept> deptList = service.findAllDept();
        //将数据存进域中
        req.setAttribute("dept",dept);
        req.setAttribute("deptList",deptList);
        req.getRequestDispatcher("/pages/system/dept/update.jsp").forward(req,resp);
    }
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个部门对象备用
        Dept pDept = new Dept();
        //请求对象需要处理一下,这里获取的是选中的所属部门的名称，给新增部门的睡属性赋值
        String pName = req.getParameter("parentId");
        pDept.setDeptName(pName);
        //调用查询所有部门方法
        List<Dept> allDept = service.findAllDept();
        //循环遍历集合
        for (Dept deptAll : allDept) {
            //如果部门的名字相同，就把id赋值给这个新建的部门对象
            if(deptAll.getDeptName().equals(pName)){
                pDept.setId(deptAll.getId());
            }
        }
        //再创建一个对象接受新增部门对象的所有的数据
        Dept dept = new Dept();
        //获取页面输入的部门名称
        String deptName = req.getParameter("deptName");
        //获取部门的状态
        String state = req.getParameter("state");
        //设置部门的父级部门id
        dept.setParentId(pDept.getId());
        //设置父级部门对象
        dept.setParent(pDept);
        //设置部门名称
        dept.setDeptName(deptName);
        //设置部门状态
        dept.setState(Integer.parseInt(state));
        //调用业务层方法，将封装好的对象作为参数使用
        int i = service.save(dept);
        //调用遍历分页方法，再到list.jsp
        this.findByPage(req, resp);
    }
    protected void findAllDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("前后通顺");
        //调用业务层方法查询所有部门
        List<Dept> list =service.findAllDept();
        //创建json工具类
        String json = new ObjectMapper().writeValueAsString(list);
        //将转换得到的json格式的字符串写回前端
        resp.setContentType("text/json;charset=UTF-8");
        resp.getWriter().write(json);
    }
    protected void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //分页查询所有部门,先给分页参数预设值
        int pageNum=1;
        int pageSize =5;
        //从前端页面获取分页参数
        String pageNumStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        if (!StringUtils.isNullOrEmpty(pageNumStr)) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (!StringUtils.isNullOrEmpty(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        List<Dept> list = service.findByPage(pageNum, pageSize);//调用业务层方法,获取返回值
        HttpSession session = req.getSession();//获取session域对象
        //调用分页根据对象
        PageInfo<Dept> page = new PageInfo<Dept>(list);
        session.setAttribute("page", page);//装货
        resp.sendRedirect(req.getContextPath() + "/pages/system/dept/list.jsp");//重定向到list页面
    }
}
